﻿using System;
using System.Collections.Generic;
using System.Text;
using nTyrant;
using System.Threading;
using System.Net;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint ep = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, 254 }), 1978);

            Client client = new Client(ep);
            byte[] Key = Encoding.ASCII.GetBytes("Test");
            
            string ValueString = new string('x', 10);
            byte[] Value = Encoding.ASCII.GetBytes(ValueString);
            DateTime start=DateTime.Now;
            bool Process = true;

            int Requests = 0;
            int Finished = 0;
            int TotalThreads = Environment.ProcessorCount * 2;
            for (int i = 0; i < TotalThreads; i++)
            {
                Thread thread=new Thread(new ThreadStart(
                    delegate
                    {
                        while (Process)
                        {
                            client.PutNoReturn(Key, Value);
                            Interlocked.Increment(ref Requests);

                            byte[] buffer = null;
                            if (client.Get(Key, out buffer))
                            {
                                Interlocked.Increment(ref Requests);
                                string Test = Encoding.ASCII.GetString(buffer);
                            }
                        }

                        Interlocked.Increment(ref Finished);
                    }));
                thread.IsBackground = true;
                thread.Priority = ThreadPriority.AboveNormal;
                thread.Start();
            }

            Thread.Sleep(1000 * 15);
            Process = false;

            while (Finished < TotalThreads)
                Thread.Sleep(250);
            TimeSpan elapsed = DateTime.Now - start;
            double index = (double)Requests;
            double requestsPerSecond = index / elapsed.TotalSeconds;
            Console.WriteLine("{0:###,###,##0.##} avg requests per second.", requestsPerSecond);
            Console.WriteLine("Press <enter> to continue");
            Console.ReadLine();
        }
    }
}
